﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class THANNHANDAO:IData<THANNHAN> 
 	{
		public THANNHANDAO(String myconnecString) : base(myconnecString) { }

		protected override int InsertOnSubmit(ref THANNHAN item, int t) {
			 SqlParameter p = new SqlParameter(); 
			 if( t == 0)
				p.Direction = ParameterDirection.Output;
			 p.ParameterName = "@" + THANNHAN.c_ID;
			 p.Value = item.ID;
			 Object obj = item.ID; 
			int i = connec.ExecuteNonQuery("sp_THANNHAN", CommandType.StoredProcedure,p,ref obj,new SqlParameter("@flag", t),
				 new SqlParameter("@" + THANNHAN.c_MANV, item.MANV), new SqlParameter("@" + THANNHAN.c_MAQH, item.MAQH),
				 new SqlParameter("@" + THANNHAN.c_TENQH, item.TENQH), new SqlParameter("@" + THANNHAN.c_TDQH, item.TDQH),
				 new SqlParameter("@" + THANNHAN.c_TIENVI, item.TIENVI), new SqlParameter("@" + THANNHAN.c_TGIAO, item.TGIAO),
				 new SqlParameter("@" + THANNHAN.c_NVID, item.NVID), new SqlParameter("@" + THANNHAN.c_QHID, item.QHID));
			if(t == 0)
				item.ID = Convert.ToInt32(obj);
			return i;
		}

		public override THANNHAN GetItem(IDataReader dr)
		{
			THANNHAN obj = new THANNHAN();
			obj.ID = Convert.ToInt32(dr[THANNHAN.c_ID]);
			obj.MANV = Convert.ToInt32(dr[THANNHAN.c_MANV]);
			obj.MAQH = Convert.ToString(dr[THANNHAN.c_MAQH]);
			obj.TENQH = Convert.ToString(dr[THANNHAN.c_TENQH]);
			obj.TDQH = Convert.ToString(dr[THANNHAN.c_TDQH]);
			obj.TIENVI = Convert.ToString(dr[THANNHAN.c_TIENVI]);
			obj.TGIAO = Convert.ToString(dr[THANNHAN.c_TGIAO]);
			obj.NVID = Convert.ToInt32(dr[THANNHAN.c_NVID]);
			obj.QHID = Convert.ToInt32(dr[THANNHAN.c_QHID]);
			return obj;
		}

		public override List<THANNHAN> GetAll()
		{
			return GetAll("sp_GetAllTHANNHAN", CommandType.StoredProcedure);
		}

		public override THANNHAN GetItemById(object id)
		{
			return GetAll().SingleOrDefault(p => p.ID == Convert.ToInt32(id));
		}
		public override THANNHAN GetItemById(THANNHAN item){
			throw new NotImplementedException();
		}

	}

 }
